package com.zrq.services.mybatis_test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zrq.core.dto.request.PageRequest;
import com.zrq.core.dto.response.APIResponse;
import com.zrq.services.mybatis_test.dto.request.SendMailRequest;
import com.zrq.services.mybatis_test.dto.request.UserRequest;
import com.zrq.services.mybatis_test.dto.result.UsersResult;
import com.zrq.services.mybatis_test.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.util.List;

/**
 * @Author: zrq
 * @CreateDate: 2020/10/28
 * @Version: 类说明： users控制层
 */
@RestController
@Slf4j
@RequestMapping("/users")
@AllArgsConstructor
@Validated
@Api(value = "users控制层",tags = "users控制层")
public class UsersController {

    private final UsersService usersService;

    @ApiOperation(value = "发送邮件",notes = "发送邮件")
    @PostMapping("/send_mail")
    public APIResponse<Boolean> sendMail(@RequestBody @Validated SendMailRequest toSend) throws MessagingException {
        return APIResponse.makeSuccess(usersService.sendMail(toSend));
    }

    @PostMapping("/users_sellist")
    public String selUsers(@RequestBody PageRequest<UserRequest> pageRequest) {

        IPage<UsersResult> resultIPage = usersService.selUsers(pageRequest);
        List<UsersResult> records = resultIPage.getRecords();
        records.forEach(System.out::println);
        return records.toString();

    }

    @PostMapping("/users_update")
    public String updateUser(@RequestBody UserRequest user) {

        Integer updateUserName = usersService.updateUserName(user);
        return ""+updateUserName;

    }

}
